package com.dingding.activity.core.activity.dao.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.activity.core.activity.dao.ICouponCityCacheDao;
import com.dingding.activity.core.activity.dao.ICouponCityDao;
import com.dingding.activity.core.activity.dao.sql.CouponCitySQL;
import com.dingding.common.core.base.daox.AbstractBaseInjectionDao;
import com.dingding.common.core.beans.activity.CouponCity;
import com.dingding.common.core.cache.CacheConstants;
import com.dingding.common.core.cache.base.JedisUtil;
import com.dingding.common.core.cache.enums.DbEnum;
import com.dingding.common.core.util.SerializableUtil;

/**
 * 
 * @Description: 优惠券关联城市DAO
 * @author CHEHUANBO
 * @date 2016年12月10日
 * @version V2.6.2
 * 
 */
@Component("couponCityDao")
public class CouponCityDaoImpl extends AbstractBaseInjectionDao implements ICouponCityDao, ICouponCityCacheDao {

	private static final Logger log = Logger.getLogger(CouponCityDaoImpl.class);

	private static boolean is_intital = true;

	@Resource(name = "couponCityDao")
	private ICouponCityDao couponCityDao;

	/**
	 * 
	 * @Description: 从数据库通过优惠券ID获取优惠券的使用城市
	 * @param couponId
	 *            城市ID
	 * @return List<CouponCity> 城市下的优惠券ID集合
	 * @author CHEHUANBO
	 * @version V2.6.2
	 * 
	 */
	@Override
	@Transactional(readOnly = true)
	public List<CouponCity> getCouponCityByCityIdToDb(int couponId) throws RuntimeException {

		if (couponId == 0) {
			throw new NullPointerException("从数据库通过优惠券ID获取优惠券的使用城市被拒绝！原因：couponId is null");
		}

		List<CouponCity> couponCitieList = super.getSqlBaseDao().listSqlToCustomBean(CouponCity.class,
				CouponCitySQL.GET_COUPON_CITY_LIST_BY_COUPONID, couponId);

		return couponCitieList;

	}

	/**
	 * 
	 * @Description: 从缓存中获取优惠券使用城市
	 * @param couponId
	 *            优惠券ID
	 * @return List<CouponCity>
	 * @author CHEHUANBO
	 * @version V2.6.2
	 * 
	 */
	@Override
	public List<CouponCity> getCouponCityToCache(int couponId) {

		if (couponId == 0) {
			throw new NullPointerException("从缓存中获取优惠券使用城市被拒绝！原因：couponId is null");
		}

		String couponIdkey = CacheConstants.COUPON_CITY_LIST + couponId;
		List<CouponCity> couponCityList = null;
		try {

			byte[] couponIdkey_byte = SerializableUtil.serialize(couponIdkey);
			if (is_intital) {
				couponCityList = couponCityDao.getCouponCityByCityIdToDb(couponId);
				JedisUtil.setValue(couponIdkey_byte, SerializableUtil.serialize(couponCityList),
						DbEnum.ActivityDb.getValue());
				is_intital = false;
			} else {
				byte[] couponCityList_byte = JedisUtil.getValue(couponIdkey_byte, DbEnum.ActivityDb.getValue());
				couponCityList = new ArrayList<CouponCity>();
				SerializableUtil.deSerialize(couponCityList_byte, couponCityList);
			}

		} catch (IOException e) {
			log.error("从缓存中获取优惠券使用城市序列化对象出现异常！优惠券ID:" + couponId, e);
		} catch (Exception e) {
			log.error("从缓存中获取优惠券使用城市出现异常！优惠券ID:" + couponId, e);
		}

		return couponCityList;
	}
	
	
	

	public static boolean isIs_intital() {
		return is_intital;
	}

	public static void setIs_intital(boolean is_intital) {
		CouponCityDaoImpl.is_intital = is_intital;
	}

	

}
